/**
 * Copyright (C) 2009 Fsti Inc.
 *
 */
package com.fsti.ppmw.port.payment;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

import com.ffcs.upcfj.map.soapServer.AServicesResp;
import com.ffcs.upcfj.map.soapServer.OrderResp;

/**
 * 返回值
 * 
 * @author Song Qing
 *
 */
public class ReturnValue {
	/**
	* Logger for this class
	*/
	private static final Logger logger = Logger.getLogger(ReturnValue.class);

	/**
	 * 能力类型
	 */
	private AbilityType abilityType;
	/**
	 * 支付结果
	 */
	private AServicesResp aServicesResp;
	/**
	 * 查询结果
	 */
	private OrderResp orderResp;
	/**
	 * 返回值
	 */
	private String value;
	/**
	 * 请求参数
	 */
	private RequestParams requestParams;
	/**
	 * 支付接口信息DAO
	 */
	private PaymentDAO paymentDAO;

	public ReturnValue(RequestParams requestParams, PaymentDAO paymentDAO) {
		super();
		this.requestParams = requestParams;
		this.paymentDAO = paymentDAO;
	}

	/**
	 * 构造函数
	 * @param value 返回值
	 */
	public ReturnValue(String value) {
		this.value = value;
	}

	public AServicesResp getaServicesResp() {
		return aServicesResp;
	}

	public void setaServicesResp(AServicesResp aServicesResp) {
		this.aServicesResp = aServicesResp;
	}

	public OrderResp getOrderResp() {
		return orderResp;
	}

	public void setOrderResp(OrderResp orderResp) {
		this.orderResp = orderResp;
	}

	@Override
	public String toString() {
		return "ReturnValue [aServicesResp=" + aServicesResp + ", orderResp=" + orderResp + "]";
	}

	/**
	 * 入库
	 * @throws DocumentException
	 */
	public void storage() throws DocumentException {
		Payment payment = paymentDAO.select(requestParams.getRequestId());
		if (payment == null) {
			return;
		}

		switch (abilityType) {
		case QUERY_ORDER:
			if (orderResp == null) {
				return;
			}
			payment.setRespCode(orderResp.getResp_code());
			String expAttach = orderResp.getExpattach();
			if (!StringUtils.isBlank(expAttach) && !"null".equalsIgnoreCase(expAttach)) {
				Document document = DocumentHelper.parseText(expAttach);
				payment.setOrderId(getNode(document, "//DATA/BASE/order_id").getText());
				payment.setOrderFee(getNode(document, "//DATA/BASE/fee").getText());
				payment.setOrderStatus(getNode(document, "//DATA/BASE/status").getText());
			}
			break;
		case PAYMENT_ORDER:
			if (aServicesResp == null) {
				return;
			}
			payment.setRespCode(aServicesResp.getResp_code());
			break;
		case QUERY_POINTS:
			if (aServicesResp == null) {
				return;
			}
			payment.setRespCode(aServicesResp.getResp_code());
			expAttach = aServicesResp.getExpattach();
			if (!StringUtils.isBlank(expAttach) && !"null".equalsIgnoreCase(expAttach)) {
				Document document = DocumentHelper.parseText(expAttach);
				payment.setBalance(getNode(document, "//DATA/EXP/balance").getText());
				payment.setOrderFee(getNode(document, "//DATA/EXP/fee").getText());
			}
			break;
		}
		paymentDAO.update(payment);
		logger.info("入库信息：" + payment);
		logger.info("                   ");
	}

	/**
	 * 获取节点
	 * 
	 * @param document
	 * @param nodePath	节点路径
	 */
	private Node getNode(Document document, String nodePath) {
		Node node = document.selectSingleNode(nodePath);
		if (node == null) {
			throw new IllegalArgumentException(nodePath + "节点不存在！");
		}
		return node;
	}

	public AbilityType getAbilityType() {
		return abilityType;
	}

	public void setAbilityType(AbilityType abilityType) {
		this.abilityType = abilityType;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}
